package cn.ygh.bbo.common.utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Desc MD5密码加密工具
 * @author zmax
 */
public class Md5Utils {
    /**
     * MD5
     *
     * @param rawPass
     * @return
     */
    public static String encodePassword(String rawPass) {
        return encodePassword(rawPass, defaultSalt);
    }

    /**
     * 加一个自定义的默认参数后，进行MD5
     *
     * @param rawPass
     * @param salt
     * @return
     */
    public static String encodePassword(String rawPass, String salt) {
        //密码为空不加密
        if (StringUtils.isBlank(rawPass)){
            return rawPass;
        }
        String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
        MessageDigest messageDigest = getMessageDigest();
        byte[] digest;
        try {
            digest = messageDigest.digest(saltedPass.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("UTF-8 not supported!");
        }
        return new String(Hex.encodeHex(digest));
    }

    /**
     * 判断是否相符
     *
     * @param encPass 加过Md5的值  c9f28491a7e915ed9c8373bcf86bd734
     * @param rawPass 原文 123456
     * @return
     */
    public static boolean isPasswordValid(String encPass, String rawPass) {
        return isPasswordValid(encPass, rawPass, defaultSalt);
    }

    /**
     * 判断密码是否相等 自定义salt
     * @param encPass
     * @param rawPass
     * @param salt
     * @return
     */
    public static boolean isPasswordValid(String encPass, String rawPass, String salt) {
        if (encPass == null) {
            return false;
        }
        String pass2 = encodePassword(rawPass, salt);
        return encPass.equals(pass2);
    }

    /**
     * 加密类
     * @return
     */
    protected static final MessageDigest getMessageDigest() {
        String algorithm = "MD5";
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("No such algorithm ["
                    + algorithm + "]");
        }
    }

    /**
     * Used by subclasses to extract the password and salt from a merged
     * <code>String</code> created using
     * {@link #mergePasswordAndSalt(String, Object, boolean)}.
     * <p>
     * The first element in the returned array is the password. The second
     * element is the salt. The salt array element will always be present, even
     * if no salt was found in the <code>mergedPasswordSalt</code> argument.
     * </p>
     *
     * @param password as generated by <code>mergePasswordAndSalt</code>
     * @return an array, in which the first element is the password and the
     * second the salt
     * @throws IllegalArgumentException if mergedPasswordSalt is null or empty.
     */
    protected static String mergePasswordAndSalt(String password, Object salt,
                                                 boolean strict) {
        if (password == null) {
            password = "";
        }
        if (strict && (salt != null)) {
            if ((salt.toString().lastIndexOf("{") != -1)
                    || (salt.toString().lastIndexOf("}") != -1)) {
                throw new IllegalArgumentException(
                        "Cannot use { or } in salt.toString()");
            }
        }
        if ((salt == null) || "".equals(salt)) {
            return password;
        } else {
            return password + "{" + salt.toString() + "}";
        }
    }

    /**
     * 混淆码。防止破解。
     */
    public static String defaultSalt = "sds22fadfdsa2314";


}
